/**
 * 服务层统一导出
 * 提供所有服务的统一访问入口
 */

// 导出基础服务
export * from './base/ipc.service'

// 导出具体服务
export * from './project.service'
export * from './app.service'
export * from './system.service'
export * from './file.service'

// 导出服务实例
export { projectService } from './project.service'
export { appService } from './app.service'
export { systemService } from './system.service'
export { fileService } from './file.service'

/**
 * 服务管理器
 * 提供统一的服务访问接口
 */
export class ServiceManager {
  // 项目服务
  static get project() {
    return import('./project.service').then(m => m.projectService)
  }

  // 应用服务
  static get app() {
    return import('./app.service').then(m => m.appService)
  }

  // 系统服务
  static get system() {
    return import('./system.service').then(m => m.systemService)
  }

  // 文件服务
  static get file() {
    return import('./file.service').then(m => m.fileService)
  }

  /**
   * 初始化所有服务
   */
  static async initialize(): Promise<void> {
    console.log('[ServiceManager] 初始化服务...')
    
    try {
      // 预加载所有服务
      await Promise.all([
        this.project,
        this.app,
        this.system,
        this.file
      ])
      
      console.log('[ServiceManager] 所有服务初始化完成')
    } catch (error) {
      console.error('[ServiceManager] 服务初始化失败:', error)
      throw error
    }
  }

  /**
   * 获取服务健康状态
   */
  static async getHealthStatus(): Promise<{
    project: boolean
    app: boolean
    system: boolean
    file: boolean
    overall: boolean
  }> {
    const status = {
      project: false,
      app: false,
      system: false,
      file: false,
      overall: false
    }

    try {
      // 测试项目服务
      const projectService = await this.project
      const projectResult = await projectService.getAllProjects()
      status.project = projectResult.success

      // 测试应用服务
      const appService = await this.app
      const appResult = await appService.getAppVersion()
      status.app = appResult.success

      // 测试系统服务
      const systemService = await this.system
      const systemResult = await systemService.getSystemInfo()
      status.system = systemResult.success

      // 测试文件服务
      const fileService = await this.file
      const fileResult = await fileService.getAppDataPath()
      status.file = fileResult.success

      // 计算总体状态
      status.overall = status.project && status.app && status.system && status.file

    } catch (error) {
      console.error('[ServiceManager] 健康检查失败:', error)
    }

    return status
  }
}

// 默认导出服务管理器
export default ServiceManager
